<?php
if (!isset($_POST['req'])) {
    die("No request provided.");
} else {
    if (filter_input(INPUT_POST, 'req') == "cpu_ram") {
        $json = array(
            'success' => false,
            'result' => array()
        );

        $wmi = new COM("WinMgmts:{impersonationLevel=impersonate}");

        $tamanio = 0;
        $cpus = $wmi->ExecQuery("SELECT LoadPercentage FROM Win32_Processor");
        $cpuPercentage = 0;
        foreach ($cpus as $cpu) :
            $cpuPercentage += $cpu->LoadPercentage;
        endforeach;

        $mems = $wmi->ExecQuery("SELECT Capacity FROM Win32_PhysicalMemory");
        foreach ($mems as $m) :
            $tamanio += $m->Capacity;
        endforeach;
        $disponible = 0;
        $disps = $wmi->ExecQuery("SELECT AvailableBytes FROM Win32_PerfRawData_PerfOS_Memory");
        foreach ($disps as $di) :
            $disponible += $di->AvailableBytes;
        endforeach;
        $diferencia = $tamanio - $disponible;
        $ram_porcentaje = ($diferencia * 100) / $tamanio;
        $ram_porcentaje1 = intval(round($ram_porcentaje));

        /*
         * TERMINA EL CODIGO DE CALCULOS PARA ENVIAR EN EL RESPONSE;
         *     
         */
        $json['success'] = true;
        $json['result'] = array('porcentajeCpu' => $cpuPercentage,
            'porcentajeRam' => $ram_porcentaje1);

        /* se envia el response */
        header('Content-Type: application/json');
        print json_encode($json);
    }
}      